package cm.tl.pms.util.fileParser.pdf;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringParser {


    public static void main(String[] args) {
        String s = "2024-06 27 2186050 2 99980 0 0 0 0 0 0\n" +
                "2024-03 35\n" +
                "2492061.1\n" +
                "7\n" +
                "2 99990 0 0 0 0 0 0\n" +
                "2023-12 39\n" +
                "4469467.1\n" +
                "7\n" +
                "2 99990 0 0 0 0 0 0\n" +
                "2023-09 42\n" +
                "4552287.1\n" +
                "7\n" +
                "2 100000 0 0 0 0 0 0\n" +
                "2023-06 36\n" +
                "4290269.1\n" +
                "5\n" +
                "0 0 0 0 0 0 0 0\n" +
                "2023-03 35\n" +
                "4519425.9\n" +
                "3\n" +
                "0 0 0 0 0 0 0 0\n" +
                "2022-12 34\n" +
                "4465421.9\n" +
                "3\n" +
                "0 0 0 0 0 0 0 0\n" +
                "2022-09 30\n" +
                "3792648.5\n" +
                "0\n" +
                "0 0 0 0 0 0 0 0\n" +
                "2022-06 28\n" +
                "3069144.5\n" +
                "0\n" +
                "0 0 0 0 0 0 0 0\n" +
                "2022-03 25\n" +
                "1836244.5\n" +
                "0\n" +
                "0 0 0 0 0 0 0 0\n" +
                "2021-12 27\n" +
                "1772240.5\n" +
                "0\n" +
                "0 0 0 0 0 0 0 0\n" +
                "2021-09 34\n" +
                "1887329.1\n" +
                "0\n" +
                "0 0 0 0 0 0 0 0";
        StringParser sp = new StringParser();
        sp.parseSum(sp.parseString(s, 11));
    }

    public List<List<String>> parseString(String input, int columns) {
        // 去掉换行符
//            String noNewLine = input.replace("\n", " ");

        // 根据空格分割成数组
        String[] parts = input.split(" ");

        // 正则表达式匹配 yyyy-MM 格式的日期
        Pattern pattern = Pattern.compile("\\d{4}-\\d{2}");
        List<List<String>> groups = new ArrayList<>();
        List<String> currentGroup = null;

        for (String part : parts) {
            if (part.isEmpty()) continue;

            Matcher matcher = pattern.matcher(part);
            if (matcher.matches()) {
                // 新的一组数据开始
                if (currentGroup != null) {
                    groups.add(currentGroup);
                }
                currentGroup = new ArrayList<>();
            }
            if (currentGroup != null) {
                if (part.length() > 9 && part.chars().filter(ch -> ch == '\n').count() == 3) {
                    currentGroup.addAll(processPart(part));
                } else {
                    currentGroup.add(part);
                }

            }
        }
        if (currentGroup != null) {
            groups.add(currentGroup);
        }

        // 确保每个组都有指定的列数
        for (List<String> group : groups) {
            while (group.size() < columns) {
                group.add("");
            }
        }

        return groups;
    }

    private List<String> processPart(String part) {
        List<String> result = new ArrayList<>();
        System.out.println("processPart: " + part);
        // 根据换行分割 把第一个和最后一个分离 中间的添加一起
        String[] splitParts = part.split("\n");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < splitParts.length; i++) {
            if (i == 1) {
                sb.append(splitParts[i]);
            } else if (i == 2) {
                result.add(sb.append(splitParts[i]).toString());
            } else {
                result.add(splitParts[i]);
            }
        }
        return result;

    }

    public String parseSum(List<List<String>> groups) {
        double sum = 0.0;
        for (List<String> group : groups) {
            System.out.println("Group: " + group);
            if (group.size() > 8) {
                String eighthElement = group.get(8);
                System.out.println("Eighth element: " + eighthElement);
                try {
                    double value = Double.parseDouble(eighthElement);
                    sum += value;
                } catch (NumberFormatException e) {
                    // 忽略非数字的情况
                    sum += 0.00;
                }
            }
        }

        // 使用 DecimalFormat 格式化结果
        DecimalFormat df = new DecimalFormat("0.00");
        String formattedSum = df.format(sum);

        System.out.println("总和: " + formattedSum);
        return formattedSum;
    }
}
